﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestServices;
using RestServices.Response.SkyDrive;
using RestServices.Response.Windows;

namespace RestServices
{
    public partial class Livelogin : Form
    {
        string Url = "";
        public WindowsLogin SkyDriveData = null;
        public Livelogin(string url)
        {
            InitializeComponent();
            Url = url;
        }

        private void Livelogin_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate(Url);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //https://login.live.com/oauth20_desktop.srf?lc=1046#access_token=EwAwAq1DBAAUlbRWyAJjK5w968Ru3Cyt/6GvwXwAAX1w%2bI9XvZLqoI0MzSXVb8V5vXbp9oA604hxc0vHyajtBPwtbyBFk6oracTq97KwWHjHcyI8cYEyskzw7EbLJhMwHIy9BN9LDE3rhlBO6FjE8XHl11Qups5rRv0PgV3SwPAg6ZLeQFPbScpnOsW8tFxBJQ/UyxEMEZEAOMUOXAFbjmOjF4lIYFnGqB2sMqS5U7BVQvhS9Is6q7p/1WKxP/FD8G/xTXLloW2cqDcdqe0fM0cqG2YZVix3Y2hZPPNFwypzHgh38bIPiYTRspIt7v01maCfQqvOd/mcrL%2b6AJTn%2bbT3qyVS2R3oio%2bEp7PDPyXtu%2bH9JzunV5xdnWJRJmQDZgAACAEsAyvXvfXbAAH1Rnedmk4VjxmQiWuw4aEy%2bQqQn98KNdsm9tDmemZG9r8uPCczb2eA9/NMjmsJQWUyYY3zNYzAEjbEHp9geroGSYDqNwAZthUbwdfUsf7fBWngEc6Du4MgyoNwkAc6mV3bW5Oth%2bGzg/cy%2b2Y9pNWa3dH%2bKdiKRK3aNBOEnRYwr%2bkWg/l2HiJx7w86QFYiesGtT82LJk1kbDf58RwckHylAIGYj98S%2bgdLaKQx%2b6jpo35s9FQkRukBiVod1iWJywv0YH/pCWe4UDQpcrj8/yqDifdGaRSzlZ5QBmPNrWqZG6U4pHIsThRu%2b0p1far/4l4paUgz2i9LHKu/4Bvg8E%2bTAAA%3d&token_type=bearer&expires_in=3600&scope=wl.signin wl.skydrive_update
            //https://login.live.com/oauth20_authorize.srf?client_id=00000000400D64AD&scope=wl.signin%20wl.basic%20wl.offline_access&redirect_uri=https://oauth.live.com/desktop
            if (webBrowser1.Url.AbsoluteUri.IndexOf("code=") > -1)
            {
                string urlRet = webBrowser1.Url.AbsoluteUri;
                SkyDriveData = new WindowsLogin();
                SkyDriveData.Code = urlRet.Substring(urlRet.IndexOf("code=") + 5);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else if (webBrowser1.Url.AbsoluteUri.IndexOf("access_token") > -1)
            {
                string urlRet = webBrowser1.Url.AbsoluteUri;
                SkyDriveData = new WindowsLogin();
                SkyDriveData.AccessToken = urlRet.Substring(urlRet.IndexOf("access_token=") + 13, urlRet.Substring(urlRet.IndexOf("access_token=") + 13).IndexOf("&"));
                SkyDriveData.ExpiresIn = urlRet.Substring(urlRet.IndexOf("expires_in=") + 11, urlRet.Substring(urlRet.IndexOf("expires_in=") + 11).IndexOf("&"));
                SkyDriveData.Scope = urlRet.Substring(urlRet.IndexOf("scope=") + 6).Replace("%20", " ");
                SkyDriveData.TokenType = urlRet.Substring(urlRet.IndexOf("token_type=") + 11, urlRet.Substring(urlRet.IndexOf("token_type=") + 11).IndexOf("&"));
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
    }
}
